<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>销售计划</span>
      </div>
      <div class="searchAdd">
        <el-button class="addBtn" type="primary" @click="ShowAddDialog">
          添加
        </el-button>
        <el-col :span="15" style="width: 400px">
          <el-date-picker
            v-model="Page.stringTime"
            type="date"
            placeholder="选择日期"
            format="yyyy年M月d日"
            value-format="yyyy/M/d"
            @change="searchByTime()"
            :editable="false"
          >
          </el-date-picker>
        </el-col>
      </div>
      <div class="InfoTable">
        <el-table
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          max-height="400px"
          stripe
        >
          <el-table-column fixed prop="saleplanId" label="ID" width="100">
          </el-table-column>
          <el-table-column prop="plannerName" label="姓名" width="130">
          </el-table-column>
          <el-table-column prop="planMonth" label="日期" width="130">
          </el-table-column>
          <el-table-column prop="statusName" label="状态" width="130">
          </el-table-column>
          <el-table-column prop="updateTime" label="最后操作时间" width="180">
          </el-table-column>
          <el-table-column prop="planOperator" label="操作人" width="130">
          </el-table-column>
          <el-table-column prop="currentName" label="处理人" >
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="160">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="showDate(scope.row)"
                type="text"
                size="small"
              >
                查看
              </el-button>
              <el-button
                @click.native.prevent="changeRow(scope.row)"
                type="text"
                size="small"
              >
                修改
              </el-button>
              <el-button
                @click.native.prevent="submitRow(scope.row)"
                type="text"
                size="small"
              >
                提交
              </el-button>
              <el-button
                @click.native.prevent="deleteRow(scope.row)"
                type="text"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :page-size="Page.size"
          :pager-count="7"
          :current-page="Page.current"
          layout="prev, pager, next"
          :page-count="Page.pageCount"
          @current-change="handleCurrentChange"
          ref="pageCurrent"
        >
        </el-pagination>
      </div>

      <!-- 添加对话框 -->
      <el-dialog
        title="添加销售计划"
        :visible.sync="AddDialogFormVisible"
        width="30%"
        @close="addDialogClosed"
      >
        <el-form
          ref="addDialogRef"
          label-width="65px"
          :model="addItem"
          :rules="addRules"
          style="padding-right: 50px"
        >
          <el-form-item label="ID" label-width="120px" prop="saleplanId">
            <el-input v-model="addItem.saleplanId"></el-input>
          </el-form-item>
          <el-form-item
            label="计划编码"
            label-width="120px"
            prop="saleplanCode"
          >
            <el-input v-model="addItem.saleplanCode"></el-input>
          </el-form-item>
          <el-form-item
            label="计划者姓名"
            label-width="120px"
            prop="plannerName"
          >
            <el-select v-model="addItem.plannerName" placeholder="请选择">
              <el-option
                v-for="item in allUser"
                :key="item.empLoginid"
                :label="item.empName"
                :value="item.empName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计划日期" label-width="120px">
            <el-input v-model="addItem.planMonth" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="计划状态" label-width="120px">
            <el-input v-model="addItem.statusName" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="计划内容" label-width="120px" prop="planContect">
            <el-input
              type="textarea"
              :rows="4"
              v-model="addItem.planContect"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addSalePlanData">添加</el-button>
          <el-button @click="AddDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 查看对话框 -->
      <el-dialog
        title="销售计划"
        :visible.sync="ShowDialogFormVisible"
        width="30%"
      >
        <el-form :model="itemDate" style="padding-right: 50px">
          <el-form-item label="计划编码" label-width="120px">
            <el-input
              v-model="itemDate.saleplanCode"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="计划者姓名" label-width="120px">
            <el-input
              v-model="itemDate.plannerName"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="计划日期" label-width="120px">
            <el-input v-model="itemDate.planMonth" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="计划状态" label-width="120px">
            <el-input v-model="itemDate.statusName" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="计划内容" label-width="120px">
            <el-input
              type="textarea"
              :rows="4"
              v-model="itemDate.planContect"
              :disabled="true"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="ShowDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog
        title="销售计划"
        :visible.sync="ChangeDialogFormVisible"
        width="30%"
      >
        <el-form :model="itemDate" style="padding-right: 50px">
          <el-form-item label="计划编码" label-width="120px">
            <el-input v-model="itemDate.saleplanCode"></el-input>
          </el-form-item>
          <el-form-item label="计划者姓名" label-width="120px">
            <el-select v-model="itemDate.plannerName" placeholder="请选择">
              <el-option
                v-for="item in allUser"
                :key="item.empLoginid"
                :label="item.empName"
                :value="item.empName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计划日期" label-width="120px">
            <el-input v-model="itemDate.planMonth" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="计划状态" label-width="120px">
            <el-input v-model="itemDate.statusName" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="计划内容" label-width="120px">
            <el-input
              type="textarea"
              :rows="4"
              v-model="itemDate.planContect"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changeSubmit()">修改</el-button>
          <el-button @click="ChangeDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
      
    </el-card>
  </div>
</template>

<script>
import pubsub from "pubsub-js";

export default {
  name: "plan",
  data() {
    return {
      // 加载状态
      loading: "",
      // 添加窗口控制器
      AddDialogFormVisible: false,
      // 查看窗口控制器
      ShowDialogFormVisible: false,
      // 修改窗口控制器
      ChangeDialogFormVisible: false,
      // 当前登录员工
      loginUserName: "",
      // 页面
      Page: {
        stringTime: null,
        current: 1,
        size: 6,
        pageCount: 1,
      },
      // 单条数据
      itemDate: {},
      // 添加数据
      addItem: {
        saleplanId: "",
        planMonth: "",
        saleplanId: "",
        saleplanCode: "",
        plannerName: "",
        statusName: "新创建",
        currentName: "",
        planOperator: "",
        planContect: "",
      },
      // 表格数据
      tableData: [],
      // 所有员工
      allUser: [],
      // 添加数据规则
      addRules: {
        saleplanId: [{ required: true, message: "请输入ID", trigger: "blur" }],
        saleplanCode: [
          { required: true, message: "请输入计划编码", trigger: "blur" },
        ],
        plannerName: [
          {
            required: true,
            message: "请选择计划者",
            trigger: ["blur", "change"],
          },
        ],
        planContect: [
          { required: true, message: "请输入计划内容", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getSalePlanData();
    pubsub.subscribe("loginUser", (_, data) => {
      this.addItem.planOperator = data;
      this.loginUserName = data;
    });
  },
  methods: {
    // 通过时间查找
    searchByTime() {
      this.getSalePlanData();
    },
    // 添加对话框
    ShowAddDialog() {
      this.addItem.planMonth = new Date().toLocaleDateString();
      this.getAllUserInfo();
      this.AddDialogFormVisible = true;
    },
    //监听添加对话框关闭事件
    addDialogClosed() {
      this.resetAddForm();
    },
    // 重置
    resetAddForm() {
      this.$refs.addDialogRef.resetFields();
    },
    // 查看对话框
    showDate(row) {
      this.itemDate = row;
      console.log(this.itemDate);
      this.ShowDialogFormVisible = true;
    },
    // 修改对话框
    changeRow(row) {
      this.getAllUserInfo();
      this.itemDate = row;
      this.ChangeDialogFormVisible = true;
    },
    // 修改
    async changeSubmit() {
      const { data: res } = await this.$http.post("/saleplan/update", {
        saleplanId: this.itemDate.saleplanId,
        statusName: this.itemDate.statusName,
        plannerName: this.itemDate.plannerName,
        planMonth: this.itemDate.planMonth,
        saleplanCode: this.itemDate.saleplanCode,
        currentName: this.itemDate.currentName,
        planOperator: this.loginUserName,
        planContect: this.itemDate.planContect,
      });
      if (res.status != 200) {
        this.loading = false;
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.ChangeDialogFormVisible = false;
      this.getSalePlanData();
    },
    // 提交
    submitRow(row) {
      if (row.statusName == "已提交") return this.$message.info("已提交");
      this.$confirm("是否提交该计划?", "提示", {
        confirmButtonText: "提交",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.post("/saleplan/commit", {
            id: row.saleplanId,
            currentName: this.loginUserName,
          });
          if (res.status != 200)
            return this.$notify.error({
              title: "失败",
              message: res.msg,
              duration: 2000,
            });
          this.getSalePlanData();
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 2000,
          });
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消提交",
            duration: 2000,
          });
        });
    },
    // 删除
    deleteRow(row) {
      this.$confirm("是否删除该计划?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.post("/saleplan/delete", {
            id: row.saleplanId,
          });
          if (res.status != 200)
            return this.$notify.error({
              title: "失败",
              message: res.msg,
              duration: 2000,
            });
          this.getSalePlanData();
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 2000,
          });
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
    // 添加销售计划
    async addSalePlanData() {
      this.$refs.addDialogRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/saleplan/addPlan",
          this.addItem
        );
        if (res.status != 200) {
          return this.$message.error(res.msg);
        }
        this.$message.success("添加成功");
        this.AddDialogFormVisible = false;
        this.getSalePlanData();
      });
    },
    // 获取表格数据
    async getSalePlanData() {
      this.loading = true;
      if (this.Page.stringTime == null) {
        const { data: res } = await this.$http.post("/saleplan/queryAllPlans", {
          current: this.Page.current,
          size: this.Page.size,
        });
        if (res.status != 200) {
          this.loading = false;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      } else {
        const { data: res } = await this.$http.post("/saleplan/getByDate", {
          currentPage: this.Page.current,
          size: this.Page.size,
          stringTime: this.Page.stringTime,
        });
        if (res.status != 200) {
          this.loading = false;
          this.Page.stringTime = null;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      }
    },
    // 页面改变
    handleCurrentChange(currentPage) {
      this.Page.current = currentPage;
      this.getSalePlanData();
    },
    // 获取所有员工信息
    async getAllUserInfo() {
      const { data: res } = await this.$http.post("/user/getAllUserInfo");
      if (res.status != 200)
        return this.$message.error("系统错误，请重新登录！");
      this.allUser = res.data;
    },
  },
};
</script>

<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-content: center;
  padding: 0 50px;
}
.searchAdd {
  width: 93%;
  margin: 0 auto;
  height: 40px;
  margin-bottom: 20px;
}

.InfoTable {
  width: 93%;
  margin: 0 auto;
}
.addBtn {
  float: left;
  width: 100px;
  height: 40px;
  margin-right: 20px;
}
</style>
